package com.liveinstars.im.endpoint;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.liveinstars.im.core.service.FriendsService;
import com.liveinstars.im.core.service.RecommendFriendService;
import com.liveinstars.im.core.service.TodoService;
import com.liveinstars.im.domain.dao.model.Friends;
import com.liveinstars.im.domain.dao.model.Todo;
import com.liveinstars.api.im.IMUserApiService;
import com.liveinstars.api.im.vo.RecommendFriendVO;
import com.liveinstars.api.im.vo.UserVO;
import com.liveinstars.im.common.exception.IMRuntimeException;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.SystemException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RestController
public class IMUserApiEndPoint implements IMUserApiService {

    @Autowired
    @Qualifier("IMUserApiServiceApiImpl")
    IMUserApiService IMUserApiService;

    @Autowired
    private FriendsService friendsService;

    @Autowired
    private TodoService todoService;

    @Autowired
    private RecommendFriendService recommendFriendService;


    @Override
    public String getUserAndUpdate(@RequestParam(value = "userId", defaultValue = "") Long userId,
                                   @RequestParam(value = "needUpdate", defaultValue = "true") Boolean needUpdate) {
        return IMUserApiService.getUserAndUpdate(userId, needUpdate);
    }

    @Override
    public String getUsersAndUpdate(@RequestParam(value = "userIds", defaultValue = "") String userIds,
                                    @RequestParam(value = "needUpdate", defaultValue = "true") Boolean needUpdate) {
        return IMUserApiService.getUsersAndUpdate(userIds, needUpdate);
    }

    @Override
    public String getMyTempRelation(@RequestParam(value = "userId", defaultValue = "") Long userId) {
        return IMUserApiService.getMyTempRelation(userId);
    }

    @Override
    public String addFriends(@RequestParam(value = "userId", defaultValue = "") Long userId,
                             @RequestParam(value = "friendsId", defaultValue = "") Long friendsId,
                             @RequestParam(value = "requestMsg", defaultValue = "") String requestMsg) {
        return IMUserApiService.addFriends(userId, friendsId, requestMsg);
    }

    @Override
    public String deleteFriends(@RequestParam(value = "userId", defaultValue = "") Long userId,
                                @RequestParam(value = "friendsId", defaultValue = "") Long friendsId) {
        return IMUserApiService.deleteFriends(userId, friendsId);
    }

    @ApiOperation(value="处理代办请求", notes="处理代办请求")
    @Override
    public String dealWithTodo(@RequestParam(value = "userId", defaultValue = "") Long userId,
							  @RequestParam(value = "todoId", defaultValue = "") Long todoId,
							  @RequestParam(value = "argeeOrNot", defaultValue = "") Boolean argeeOrNot,
						@RequestParam(value = "handleMsg", defaultValue = "") String handMsg) {
        Result<Todo> result = new Result<>();
        try {
            Todo todo = todoService.dealWithTodo(userId, todoId, argeeOrNot, handMsg);
            result.setData(todo);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else if (e instanceof SystemException){
                result.setCode(((SystemException) e).getCode());
                result.setMessage(e.getMessage());
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return JSON.toJSONString(result);
    }

    @Override
    public String shieldUser(@RequestParam(value = "userId", defaultValue = "") Long userId,
                             @RequestParam(value = "toId", defaultValue = "") Long toId) {
        return IMUserApiService.shieldUser(userId, toId);
    }

    @Override
    public String remarkUser(@RequestParam(value = "userId", defaultValue = "") Long userId,
                             @RequestParam(value = "toId", defaultValue = "") Long toId,
                             @RequestParam(value = "remark", defaultValue = "") String remark) {
        return IMUserApiService.remarkUser(userId, toId, remark);
    }

    @Override
    public String iplist() {
        return IMUserApiService.iplist();
    }

    @Override
    public Result<List<UserVO>> getRecommendUsers(@RequestParam(value = "userId", defaultValue = "") Long userId) {
        Result<List<UserVO>> result = new Result<>();
        List<RecommendFriendVO> list = recommendFriendService.listAll();
        List<UserVO> recommendFriendList = Lists.newArrayList();
        List<Friends> friendList = friendsService.listByUserIdAndType(userId, 1);
        List<Long> friendIdList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(list)) {
            if (CollectionUtils.isNotEmpty(friendList)) {
                friendIdList = friendList.stream().map(Friends::getFriendId).collect(Collectors.toList());
            }
            for (RecommendFriendVO vo : list) {
                UserVO userVO = new UserVO();
                userVO.setId(vo.getUserId());
                userVO.setLogo(vo.getLogo());
                userVO.setNickName(vo.getUserName());
                userVO.setName(vo.getUserName());
                userVO.setDate(vo.getCreateTime().toString());
                userVO.setGroupId(vo.getGroupId());
                userVO.setIndividualResume(vo.getIndividualResume());
                if(friendIdList.indexOf(vo.getUserId()) >= 0) {
                    userVO.setFriend(1);
                } else {
                    userVO.setFriend(0);
                }
                recommendFriendList.add(userVO);
            }
        }
        result.setData(recommendFriendList);
        return result;
    }

    @Override
    public String getRelateUserJson(@RequestParam(value = "userId", defaultValue = "") Long userId) {
        return IMUserApiService.getRelateUserJson(userId);
    }

    @Override
    public Result<Boolean> isFriend(@RequestParam(value = "userId", defaultValue = "") Long userId,
                                     @RequestParam(value = "myId", defaultValue = "") Long myId) {
        return IMUserApiService.isFriend(userId, myId);
    }

    @Override
    public Result<Boolean> isShield(@RequestParam(value = "userId", defaultValue = "") Long userId,
                                     @RequestParam(value = "myId", defaultValue = "") Long myId) {
        return IMUserApiService.isShield(userId, myId);
    }

    @Override
    public Result<String> getRemarkName(@RequestParam(value = "userId", defaultValue = "") Long userId,
                                     @RequestParam(value = "myId", defaultValue = "") Long myId) {
        return IMUserApiService.getRemarkName(userId, myId);
    }

    @Override
    public Result<List<UserVO>> listServiceUser() {
        return IMUserApiService.listServiceUser();
    }
}
